package il.technion.cs236369.webserver.typeHandlers;

import il.technion.cs236369.interfaces.IObservable;
import il.technion.cs236369.interfaces.IObserver;
import il.technion.cs236369.interfaces.ISession;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * A basic session implementation
 * 
 * @author Gal Lalouche
 * 
 */
class SessionImpl implements ISession, IObservable {
	private final Map<String, Object>	map	= new ConcurrentHashMap<String, Object>();
	private IObserver					observer;

	public SessionImpl() {
		super();
	}

	@Override
	public void set(String name, Object value) {
		map.put(name, value);
	}

	@Override
	public void invalidate() {
		map.clear();
		if (observer != null) {
			observer.notifyObserver();
		}
	}

	@Override
	public Object get(String name) {
		return map.get(name);
	}

	/**
	 * Sets the observable to be called when the session is invalidated
	 */
	@Override
	public void setObservable(IObserver observer) {
		this.observer = observer;
	}
}